/**
 * @author Gaurav Ramanan
 */

(function( $ )
{
	$.fn.rcfx = function(args)
	{
		var Settings = 
		{
			'animationImage':	'none',
			'staticImage':		'none',
			
			'pendulum' : 		 true,
			'orientation' : 	'horizontal',
			
			'numberOfFrames' : 	 5,
			'frameHeight': 		'0px',
			'frameWidth': 		'0px',
			'framesPerSecond' :  null,
			'duration' :		 null,
			
			'shiftLeft': 		'0px',
			'shiftTop':			'0px',
			
			'width' : 			'0px',
			'height' : 			'0px',
			
			'toLeft':			null,
			'toTop' : 			null,
			'fromLeft':			null,
			'fromTop' :			null,			
			
			'direction_' :		'right',
			'frame_' :			0
		};
		
		var Methods = 
		{
			init: function (Options)
			{
				return this.each(function () 
				{
					var $this = $(this);
					
					//if ($this.css('width')) Settings.frameWidth = $this.css('width');					
					if ($this.css('height')) Settings.frameHeight = $this.css('height');
					
					Settings.frameWidth = $this.css('width') || Settings.frameWidth;
						
					//$this.css('background-attachment', 'fixed');

						Settings.frame = Settings.start
					if (Options)
						$.extend(Settings, Options);
					
					//Associate settings to every object
					$this.data(Settings);
				});
			},
			
			play : function ()
			{
				return this.each(function () 
				{
					var $this = $(this);
					
					//No fps is defined
					if ($this.data('framesPerSecond') == null)
					{
						//No FPS but a duration is defined
						if ($this.data('duration') != null)
						{
							$this.data('framesPerSecond', ($this.data('numberOfFrames') / $this.data('duration')) * 1000 );
						}
					}
					
					//Sets the from / to / left / top. If it isnt defined will take the css of its left and top
					if (!$this.data('fromTop')) 
						$this.data('fromTop', $this.css('top'))
						
					if (!$this.data('fromLeft')) 
						$this.data('fromLeft', $this.css('left'))
						
					if (!$this.data('toTop')) 
						$this.data('toTop', $this.css('top'))
						
					if (!$this.data('toLeft')) 
						$this.data('toLeft', $this.css('left'))
						
					//Make sure 'px' is added
					$this.data('fromTop',  parseInt($this.data('fromTop')) + 'px');
					$this.data('fromLeft', parseInt($this.data('fromLeft')) + 'px');
					$this.data('toTop', parseInt($this.data('toTop')) + 'px');
					$this.data('toLeft', parseInt($this.data('toLeft')) + 'px');
						
						 
					//Animate the motion of the element
					$this.animate({top: $this.data('toTop'), left : $this.data('toLeft')}, $this.data('duration') );					
					
					var t = setInterval(function () {_proceedFrame($this)}, 1000 / $this.data('framesPerSecond'));
					$this.data('timer_', t); 
				});
			},
			
			pause : function ()
			{
				return this.each(function () 
				{
					var $this = $(this);
					clearInterval($this.data('timer_'));
				});
			},
			
			stop : function ()
			{
				return this.each(function () 
				{
					var $this = $(this);
				});
			}
		};

		// Method calling logic
		if ( Methods[args] ) 
			return Methods[args].apply( this, Array.prototype.slice.call( arguments, 1 ));
			
		else if ( typeof args === 'object' || ! args ) 
			return Methods.init.apply( this, arguments );
		
		else 
			$.error( 'Method ' +  args + ' does not exist on jQuery.tooltip' );    
	};
	
})( jQuery );
